# test01_requests.py
# 1. 下载新浪首页数据
# 2. 使用正则匹配所有图片的url,保存到列表中
# 3. 遍历列表,下载图片
import requests, re, os
def download(url, fname):
    resp = requests.get(url)
    with open(fname, mode="wb") as fw:
        fw.write(resp.content)
def get_url(fname, re_str):  # fname文件路径  re_str正则字符串
    pic_list = []  # 保存图片url地址
    re_obj = re.compile(re_str)  # 正则字符串编译正则对象
    with open(fname, mode="r") as fr:
        for line in fr.readlines():  # line: 每一行
            data = re_obj.search(line)
            if data != None:  # 匹配到了数据
                pic_list.append(data.group())
    return pic_list
if __name__ == '__main__':
    fname, pic_dir = "/opt/sina.html", "/tmp/mypics"
    re_str = "(http|https)://[\w\./-]+\.(jpg|png|jpeg|gif)"
    if os.path.exists(fname) == False:  # 文件不存在则下载
        download("https://www.sina.com.cn/", fname)
    if os.path.exists(pic_dir) == False:  # 目录不存在则创建
        os.mkdir(pic_dir)
    tmp = get_url(fname, re_str)
    for pic_url in tmp:
        download(pic_url, f"{pic_dir}/{pic_url.split('/')[-1]}")








